package com.hitangjun.rel;

import static com.hitangjun.rel.ControlPC.*;
import static com.hitangjun.utils.MessageUtils.*;
import static com.hitangjun.utils.RxConvertHelper.intToByteArray2;

/**
 * @create 2021-02-01 22:37
 * 封装报文
 **/
public class MessagePotting {

    /**
     * 封装报文 PC到设备
     * 报文小于60字节 封装报文 PC向设备 一次发送完，不需要续传
     * @return
     */
    public static byte[] pottingPcToEquipmentMessage(byte[] commond,byte[] data){
        //报文长度=头部1字节+地址1字节+命令2字节+data长度
        byte[] bytes = new byte[4 + data.length];
        bytes[0] = HEAD;
        bytes[1] = ADDRESS_SERVER_TO_PC;
        //命令
        bytes[2] = commond[0];
        bytes[3] = commond[1];
        //数据大小 数据 校验位
        for (int i = 0, k = 4; i < data.length; i++, k++) {
            bytes[k] = data[i];
        }
        return bytes;
    }


    /**
     * 报文续传的方法
     * @param data
     */
    public static void sendSequel(byte[] command,byte[] data){
        //传过来的总长
        int totalMessageLength = data.length;
        //数据总长度
        int totalDataLength = totalMessageLength - 4;
        //分成的报文个数
        int totalNumber = (totalDataLength % MAX_DATA_LENGTH == 0) ? totalDataLength/MAX_DATA_LENGTH : totalDataLength/MAX_DATA_LENGTH + 1;

        //总数据
        byte[] totalData = subBytes(data,2,totalDataLength);
        //第一段
        for (int i = 1; i <= 3 ; i++) {
            if (i == 1){
                byte[] dataBegin = new byte[52];
                byte[] dataSize = intToByteArray2(dataBegin.length);
                int crcInt = 311;
                byte[] crc = intToByteArray2(crcInt);
                byte[] messageBegin = pottingSequelMessage(command, dataSize, dataBegin, crc);
                //调用方法把第一条报文发出去
                //sendRequestToEquipment(messageBegin);
            }else if(i != 1 && i<totalNumber){
                //第i次
                //续传命令
                byte[] commandMiddle = intToByteArray2(CONTINUE_LOAD);
                //数据大小 为满数组大小
                byte[] dataSize = intToByteArray2(MAX_DATA_LENGTH);
                //数据内容 通过截取原数据数组
                byte[] dataMiddle = subBytes(totalData,(i-1)*MAX_DATA_LENGTH,MAX_DATA_LENGTH);
                //校验码
                int crcInt = 311;
                byte[] crc = intToByteArray2(crcInt);

                byte[] messageMiddle = pottingSequelMessage(commandMiddle,dataSize,dataMiddle,crc);
                //调用方法把第一条报文发出去
                //sendRequestToEquipment(messageMiddle);

            }else if(i == totalNumber){
                //最后一次
                //续传结束命令
                byte[] commandLast = intToByteArray2(CONTINUE_LOAD_END);
                //数据大小 为满数组大小
                int lastMassageLength = totalDataLength - ((totalNumber-1)*MAX_DATA_LENGTH);
                byte[] dataSize = intToByteArray2(lastMassageLength);
                //数据内容 通过截取原数据数组
                byte[] dataLast = subBytes(totalData,(i-1)*MAX_DATA_LENGTH,lastMassageLength);
                //校验码
                int crcInt = 311;
                byte[] crc = intToByteArray2(crcInt);

                byte[] messageLast = pottingSequelMessage(commandLast,dataSize,dataLast,crc);
                //调用方法把第一条报文发出去
                //sendRequestToEquipment(messageLast);
            }

        }

    }

    /**
     * 封装续传报文
     * @param commond
     * @param dataSize
     * @param data
     * @param crc
     * @return
     */
    public static byte[] pottingSequelMessage(byte[] commond,byte[] dataSize,byte[] data,byte[] crc){
        //报文长度=头部1字节+地址1字节+命令2字节+大小2字节+crc2字节data长度
        byte[] bytes = new byte[8 + data.length];
        bytes[0] = HEAD;
        bytes[1] = ADDRESS_SERVER_TO_PC;
        //命令
        bytes[2] = commond[0];
        bytes[3] = commond[1];
        //数据大小
        bytes[4] = dataSize[0];
        bytes[5] = dataSize[1];
        //数据 校验位
        for (int i = 0, k = 6; i < data.length; i++, k++) {
            bytes[k] = data[i];
        }
        //校验位
        bytes[bytes.length-2] = crc[0];
        bytes[bytes.length-1] = crc[1];

        return bytes;
    }
}
